<?php
class Tourism_goods_base_model extends BZ_Model
{
    protected $table    = 'tourism_goods_base';
    protected $table_2  = 'tourism_goods_attr';
    protected $table_4  = 'user';

    /**
     * 搜索条件 商品状态is_check 商品名称goods_name is_on_sales上架下架
     * $param array()
     * */
    public function total($param = array())
    {
        $this->db->from($this->table.' as b');
        $this->db->join($this->table_2.' as a','a.goods_id = b.goods_id','left');
        if (!empty($param['is_on_sale'])) {
            $this->db->where('b.is_on_sale', $param['is_on_sale']);
        }
        if (!empty($param['is_check'])) {
            $this->db->where('a.is_check', $param['is_check']);
        }
        if (!empty($param['is_beizhu'])) {
            $this->db->where('a.is_beizhu', $param['is_beizhu']);
        }
        if (!empty($param['goods_id'])) {
            $this->db->where('b.goods_id', $param['goods_id']);
        }
        if (!empty($param['goods_name'])) {
            $this->db->like('b.goods_name', $param['goods_name']);
        }
        if (!empty($param['category_name'])) {
            $this->db->like('b.category_name', $param['category_name']);
        }
        if (!empty($param['uid'])) {
            $this->db->where('b.uid', $param['uid']);
        }
        $this->db->where('a.logic_delete',1);
        return $this->db->count_all_results();
    }

    public function page_list($page_num, $num, $param = array())
    {
        $this->db->select('b.*,a.goods_attr_id,a.is_beizhu,a.is_check,a.attribute_value,a.created_at,a.supply_price,a.shop_price,a.goods_num,a.sale_count');
        $this->db->from($this->table.' as b');
        $this->db->join($this->table_2.' as a', 'a.goods_id = b.goods_id','left');
        if (!empty($param['is_on_sale'])) {
            $this->db->where('b.is_on_sale', $param['is_on_sale']);
        }
        if (!empty($param['is_check'])) {
            $this->db->where('a.is_check', $param['is_check']);
        }
        if (!empty($param['is_beizhu'])) {
            $this->db->where('a.is_beizhu', $param['is_beizhu']);
        }
        if (!empty($param['goods_id'])) {
            if (is_array($param['goods_id'])) {
                $this->db->where_in('b.goods_id', $param['goods_id']);
            } else {
                $this->db->where('b.goods_id', $param['goods_id']);
            }
        }
        if (!empty($param['goods_name'])) {
            $this->db->like('b.goods_name', $param['goods_name']);
        }
        if (!empty($param['category_name'])) {
            $this->db->like('b.category_name', $param['category_name']);
        }

        if (!empty($param['uid'])) {
            $this->db->where('b.uid', $param['uid']);
        }

        $this->db->where('a.logic_delete',1);
        $this->db->order_by('a.goods_attr_id', 'desc');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    /**
     *商品添加 暂时为一对一
     */
    public function insertGoodsBase($postData)
    {
        $data = array(
            'category_id'   => $postData['category_id'],
            'category_name' => trim($postData['category_name']),
            'uid'           => $postData['uid'],
            'goods_name'    => trim($postData['goods_name']),
            'brand_id'      => !empty($postData['brand_id']) ? $postData['brand_id'] : 0,
            'province_id'   => $postData['province_id'],
            'city_id'       => $postData['city_id'],
            'district_id'   => $postData['district_id'],
            'payments'      => (count($postData['payments']) == 2) ? 2 : 1,
            'is_on_sale'    => $postData['is_on_sale'],
            'goods_brief'   => $postData['goods_brief'], //商品简介
            'images'        => $postData['images'],
            'transport_id'  => !empty($postData['transport_id']) ? $postData['transport_id'] : 0,
            'transport_cost'=> !empty($postData['transport_cost']) ? $postData['transport_cost'] : 0,
            'limit_num'     => !empty($postData['limit_num']) ? $postData['limit_num'] : 0,
            'limit_day'     => !empty($postData['limit_day']) ? $postData['limit_day'] : 0,
            'goods_native'  => $postData['goods_native'],
            'rate_id'       => $postData['rate_id']
        );
        //设置省市区 详细地址
        if (!empty($postData['detail'])) {
            $data['detail'] = $this->region->getRegionNames([$postData['province_id'], $postData['city_id'], $postData['district_id']]).' '.$postData['detail'];
        }
        //商品附加属性
        if (!empty($postData['key']) && !empty($postData['value'])) {
            $data['goods_spec'] = $this->get_good_spec($postData['key'], $postData['value']);
        } else {
            $data['goods_spec'] = '';
        }
        //运费模板信息
        if (isset($postData['transport_type']) && $postData['transport_type'] == 1) {//使用邮费模版
            $data['transport_id']   = $postData['transport_id']; 
            $data['transport_cost'] = 0;
        } else {
            $data['transport_id']   = 0;
            $data['transport_cost'] = $postData['transport_cost'];
        }
        //商品供应商分类
        if( !empty($postData['provider_category_id']) ){
        	$data['provider_category_id'] = $postData['provider_category_id'];
        }
        
        if( !empty($postData['wap_goods_desc'])){
            $data['wap_goods_desc'] = $postData['wap_goods_desc'];
        }
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function updateGoodsBase($postData)
    {
        $data = array(
            'category_name' => trim($postData['category_name']),
            'goods_name'    => trim($postData['goods_name']),
            'brand_id'      => !empty($postData['brand_id']) ? $postData['brand_id'] : 0,
            'province_id'   => $postData['province_id'],
            'city_id'       => $postData['city_id'],
            'district_id'   => $postData['district_id'],
            'payments'      => (count($postData['payments']) == 2) ? 2 : 1,
            'is_on_sale'    => $postData['is_on_sale'],
            'goods_brief'   => $postData['goods_brief'], //商品简介
            'images'        => $postData['images'],
            'transport_id'  => !empty($postData['transport_id']) ? $postData['transport_id'] : 0,
            'transport_cost'=> !empty($postData['transport_cost']) ? $postData['transport_cost'] : 0,
            'limit_num'     => !empty($postData['limit_num']) ? $postData['limit_num'] : 0,
            'limit_day'     => !empty($postData['limit_day']) ? $postData['limit_day'] : 0,
            'goods_native'  => $postData['goods_native'],
            'rate_id'       => $postData['rate_id']
        );

        //设置省市区 详细地址
        if (!empty($postData['detail'])) {
            $data['detail'] = $this->region->getRegionNames([$postData['province_id'], $postData['city_id'], $postData['district_id']]).' '.$postData['detail'];
        }
        //商品附加属性
        if (!empty($postData['key']) && !empty($postData['value'])) {
            $data['goods_spec'] = $this->get_good_spec($postData['key'], $postData['value']);
        } else {
            $data['goods_spec'] = '';
        }
        //运费模板信息
        if (isset($postData['transport_type']) && $postData['transport_type'] == 1) {//使用邮费模版
            $data['transport_id']   = $postData['transport_id'];
            $data['transport_cost'] = 0;
        } else {
            $data['transport_id']   = 0;
            $data['transport_cost'] = $postData['transport_cost'];
        }
        //商品供应商分类
        if( !empty($postData['provider_category_id']) ){
            $data['provider_category_id'] = $postData['provider_category_id'];
        }
        if( !empty($postData['wap_goods_desc'])){
            $data['wap_goods_desc'] = $postData['wap_goods_desc'];
        }
        $this->db->where('goods_id', $postData['goods_id']);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 获取商品类型
     * @param unknown $keys
     * @param unknown $value
     * @return string
     */
    public function get_good_spec($keys, $value)
    {
        if ($keys && $value) {
            foreach ($keys as $k => $key) {
                if (!isset($keys[$k]) || empty($keys[$k]) || !isset($value[$k]) || empty($value[$k])) {
                    unset($keys[$k]);
                    unset($value[$k]);
                }
            }
            return serialize(array_combine($keys, $value));
        }
        return '';
    }
    
    /*
     *更新商品状态
     * */
    public function updateGoodsStatus($params)
    {
        $data = array(
            'is_on_sale' => $params['is_on_sale']
        );
        $this->db->where('uid', $params['uid']);
        $this->db->where('goods_id', $params['goods_id']);
        return $this->db->update($this->table, $data);
    }
    
    
    /*
    *获取订单商品
    * */
    public function getOrderGoods($order_id = 0)
    {
        if (!$order_id) {
            return '';
        }
        $this->db->where('b.goods_id', $order_id);
    }
    
    /*
   *获取商品信息
   */
    public function getGoodsAttr($goods_attr_id)
    {
        $this->db->select('tourism_goods_base.*, user.alias_name, tourism_goods_attr.*');
        $this->db->from($this->table_2 . ' AS tourism_goods_attr');
        $this->db->where('tourism_goods_attr.goods_attr_id', $goods_attr_id);
        $this->db->join($this->table . ' AS tourism_goods_base', 'tourism_goods_base.goods_id = tourism_goods_attr.goods_id');
        $this->db->join($this->table_4 . ' AS user', 'user.uid = tourism_goods_base.uid');
        return $this->db->get();
    }
    /**
     * 获取商品的SKU对应的规格
     * author laona
     */
    public function attribute($postData = array(), $str = '')
    {
        foreach ($postData as $item) {
            $str[] = $this->tourism_goods_category_spec->getSpecNameById($item);
        }
        return implode(',', $str);
    }
    
    /**
     * 根据供应商分类获取商品总数
     * @param unknown $id
     */
    public function findResultByProviderCategoryId($id)
    {
         $this->db->select('goods_id');
         $this->db->from('tourism_goods_base');
         $this->db->where('provider_category_id',$id);
         return $this->db->get();
    }
}